//
//  DateTool.m
//  Conference
//
//  Created by 陈鑫 on 12-11-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DateTool.h"

@implementation DateTool
static DateTool *sharedManager=nil;

-(id)init{
	if (self =[super init]) {

        
    }
    return self;
}

+(DateTool *)getInstance{
	@synchronized(self){
		if(sharedManager ==nil){
			sharedManager=[[self alloc] init];
		}
	}
	return sharedManager;
}
-(NSDate*)getDateForString:(NSString *)dateStr{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd"]; 
    NSDate *date=[dateFormatter dateFromString:dateStr];
    [dateFormatter release];
    return date;
}

-(NSString*)getTimeByDuration:(NSString*)time{
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    long i=[[NSDate date]timeIntervalSince1970]-[[dateFormatter dateFromString:time]timeIntervalSince1970];
    NSString *str=@"";
    long m=1000*60*1;
    long h=1000*60*60*1;
    if(i<m){
        
        str=@"刚刚";
        
    }else if(i<h){
        str=[NSString stringWithFormat:@"%d分钟前",i/m];
        
    }else if(i<h*24*1){
        
          str=[NSString stringWithFormat:@"%d小时前",i/h];
        
    }else if (i<h*24*2) {
        
          str=[NSString stringWithFormat:@"昨天"];
        
    }else if (i<h*24*3) {
        str=[NSString stringWithFormat:@"前天"];
        
    }else {
        str=[dateFormatter stringFromDate:[dateFormatter dateFromString:time]];
    }
    return str;
}

-(NSString*)getTimeByDurationForString:(NSString*)time{
    
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    
    NSDate* date=[dateFormatter dateFromString:time];
    
    NSTimeInterval timeLong=[date timeIntervalSince1970];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    
    return [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeLong]];
}


-(NSString*)getNowDate{
    
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd"]; 
    return [dateFormatter stringFromDate:[NSDate date]]; 
}
-(NSString*)getNowTime{
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    return [dateFormatter stringFromDate:[NSDate date]];
}
@end
